<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>添加文件</title>
    <meta name="renderer" content="webkit">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
</head>
<body>

<div style="padding: 20px 50px 10px 50px">
    <div class="layui-progress layui-progress-big" lay-filter="demo" lay-showPercent="true">
        <div class="layui-progress-bar" lay-filter="demo" lay-percent="0%"></div>
    </div>
</div>

</fieldset>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;text-align: center">
    <legend>拖拽上传</legend>
    <div style="padding: 20px">
        <div class="layui-upload-drag" id="test10">
            <i class="layui-icon"></i>
            <p>点击上传，或将文件拖拽到此处</p>
            <div class="layui-hide" id="uploadDemoView">
                <hr>
                <img src="" alt="上传成功后渲染" style="max-width: 196px">
            </div>
        </div>
    </div>

</fieldset>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;text-align: center">
    <legend>选完文件后不自动上传</legend>
    <div style="padding: 20px">
        <div class="layui-upload">
            <button type="button" class="layui-btn layui-btn-normal" id="test8">
                <i class="layui-icon">&#xe67c;</i>选择文件
            </button>
            <button type="button" class="layui-btn" id="test9">开始上传</button>
            <button type="button" class="layui-btn" id="fileInfoLink">详情</button>
        </div>
    </div>

</fieldset>


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;text-align: center">
    <legend>高级应用：制作一个多文件列表</legend>
    <div style="padding: 20px">

        <div class="layui-upload">
            <div class="layui-upload-list">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>文件名</th>
                        <th>大小</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="demoList"></tbody>
                </table>
            </div>
            <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
            <button type="button" class="layui-btn" id="testListAction">开始上传</button>
        </div>
    </div>
</fieldset>


<script src="/static/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['form', 'layedit', 'laydate', 'upload', 'element'], function () {
        var form = layui.form
            , $ = layui.$
            , upload = layui.upload
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        var uploadUrl = "../rest/file/add";

        var element = layui.element;


        var xhrOnProgress = function (fun) {
            xhrOnProgress.onprogress = fun; //绑定监听
            //使用闭包实现监听绑
            return function () {
                //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
                var xhr = $.ajaxSettings.xhr();
                //判断监听函数是否为函数
                if (typeof xhrOnProgress.onprogress !== 'function')
                    return xhr;
                //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
                if (xhrOnProgress.onprogress && xhr.upload) {
                    xhr.upload.onprogress = xhrOnProgress.onprogress;
                }
                return xhr;
            }
        }


        //选完文件后不自动上传
        upload.render({
            elem: '#test8'
            , url: uploadUrl //改成您自己的上传接口
            , auto: false
            , accept: 'file'
            , size: 1024 * 100
            //,multiple: true
            , bindAction: '#test9'
            , done: function (res) {
                if (res.code == "200") {
                    layer.msg('上传成功');
                    $("#test8").find(":input").value = "";
                } else {
                    layer.msg('上传失败');
                }
                console.log(res)
            }, xhr: xhrOnProgress,
            progress: function (value) {//上传进度回调 value进度值
                element.progress('demo', value + '%');
            }
        });

        //拖拽上传
        upload.render({
            elem: '#test10'
            , url: uploadUrl //改成您自己的上传接口
            , accept: 'file'
            , size: 1024 * 100
            , done: function (res) {
                layer.msg('上传成功');
                console.log(res)
            }, xhr: xhrOnProgress,
            progress: function (value) {//上传进度回调 value进度值
                element.progress('demo', value + '%');
            }
        });


        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: uploadUrl //改成您自己的上传接口
            , accept: 'file'
            , size: 1024 * 100
            , multiple: true
            , auto: false
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            , done: function (res, index, upload) {
                if (res.code) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html('<button id="fileInfoLink" class="layui-btn layui-btn-xs demo-info" file-id=' + 0 + ' >详情</button>'); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            , error: function (index, upload) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }, xhr: xhrOnProgress,
            progress: function (value) {//上传进度回调 value进度值
                element.progress('demo', value + '%');
            }
        });

    });
</script>

</body>
</html>